//
//  XSComposeToolBar.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/14.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

/// 定义不同按钮的枚举
///
/// - Picture:  图片
/// - Mention:  @
/// - Trend:    话题
/// - Emoticon: 表情图标
/// - Add:      加号
enum XSComposeToolBarButtonType:Int{
    case Picture = 0,Mention,Trend,Emoticon,Add
}

class XSComposeToolBar: UIStackView {

    //按钮点击时调用的闭包
    var composeToolBarBtnDidClick:((type:XSComposeToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        
        backgroundColor = UIColor.orangeColor()
        //设置大小相等且填充父控件
        distribution = .FillEqually
        
        
        addChildButton("compose_add_background", type: XSComposeToolBarButtonType.Picture)
        addChildButton("compose_mentionbutton_background", type: XSComposeToolBarButtonType.Mention)
        addChildButton("compose_trendbutton_background", type: XSComposeToolBarButtonType.Trend)
        addChildButton("compose_emoticonbutton_background", type: XSComposeToolBarButtonType.Emoticon)
        addChildButton("compose_add_background", type: XSComposeToolBarButtonType.Add)
    }
    
    private func addChildButton(imageNamed:String,type:XSComposeToolBarButtonType){
        
        //初始化button
        let button = UIButton()
        
        //监听点击事件
        button.addTarget(self, action: "ChildButtonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //添加不同状态图片
        button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        //添加背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //添加到stackView中
        addArrangedSubview(button)
        
    }
    
    //ToolBar按钮点击事件
    @objc private func childButtonClick(button:UIButton){
        
        //调用外界传入的闭包
        composeToolBarBtnDidClick?(type:XSComposeToolBarButtonType(rawValue: button.tag)!)
    }
}
